<template>
  <div>
    <van-nav-bar fixed>
      <template #left>
        <img :src="logo" alt="" class="hmlogo" />
      </template>
      <template #right>
        <van-icon name="search" size="0.48rem" color="#fff" @click="$router.push('/search')"/>
      </template>
    </van-nav-bar>
    <van-tabs v-model="channelId" animated sticky offset-top="1.26rem">
      <van-tab
        v-for="item in channelList"
        :title="item.name"
        :key="item.id"
        :name="item.id"
      >
        <article-list :cid="channelId"></article-list>
      </van-tab>
    </van-tabs>
    <!-- 编辑频道图标 -->
    <van-icon name="plus" size="0.37333334rem" class="moreChannels" />

    <!-- 弹出层组件 -->
    <van-icon
      name="plus"
      size="0.37333334rem"
      class="moreChannels"
      @click="show = true"
    />
    <van-popup v-model="show" class="edit_wrap"   @closed="onPopupClosed">
      <!-- 弹出层的主体区域 -->
      <channel-edit
        @close="show = false"
        :userChannelList="channelList"
        @addChannel="addChannelFn"
        @removeChannel="removeChannelFn"
        ref="channelEdit"
        @changeChannel="changeChannelFn"
      ></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import ChannelEdit from './ChannelEdit.vue'
import imglogo from '../../assets/toutiao_logo.png'
import { userChannelAPI, updateChannelAPI } from '@/api'
import ArticleList from '../Home/ArticleList.vue'
export default {
  components: { ArticleList, ChannelEdit },
  name: 'Home',
  data () {
    return {
      logo: imglogo,
      channelList: [],
      channelId: 0,
      show: false // 编辑频道弹出层显示
    }
  },
  methods: {
    changeChannelFn (val) {
      this.channelId = val.id
    },
    onPopupClosed () {
      this.$refs.channelEdit.isEdit = false
    },
    async updateChannel () {
      // 目标：
      // 将更新后的数据传入到后端接口
      // 根据接口所需要的数据格式传递参数(后端规定的)
      // 1、需要去掉推荐频道
      // 2、需要将seq添加到每一个对象中seq是当前的顺序index+1
      // 3、传入到后端数据中不能有name需要delete name
      // 4、调用接口的时候需要在外层包一个channels
      const list = this.channelList.filter((item) => item.id !== 0)
      const resArr = list.map((item, index) => {
        const newObj = { ...item }
        newObj.seq = index + 1
        delete newObj.name
        return newObj
      })
      console.log(resArr)
      await updateChannelAPI({
        channels: resArr
      })
    },
    // 添加频道
    addChannelFn (val) {
      this.channelList.push(val)
      this.updateChannel()
    },
    // 删除频道
    removeChannelFn (val) {
      const index = this.channelList.findIndex((item) => item.id === val.id)
      this.channelList.splice(index, 1)
      this.updateChannel()
    }
  },
  async created () {
    const res = await userChannelAPI()
    this.channelList = res.data.data.channels
  }
}
</script>

<style scoped lang="less">
.hmlogo {
  width: 100px;
  height: 30px;
}

/* tab内容距离tab导航的距离 */
/deep/ .van-tabs__content {
  padding-top: 44px;
}

/deep/ .van-tabs__wrap {
  padding-right: 30px;
  background-color: #fff;
}

.moreChannels {
  position: fixed;
  top: 62px;
  right: 8px;
  z-index: 999;
}
/* 频道编辑弹出层铺满屏幕 */
.edit_wrap {
  width: 100%;
  height: 100%;
}

</style>
